
package go.tetris.client;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

public final class Onoo {
	private static Integer bonus = 0;
	private static Integer tootsoh = 0;
	private static Integer time = 0;
	private static Integer mor = 0;
	private static Integer onoo = 0;
	private static Integer speed = 1000;
	private static VerticalPanel panel = new VerticalPanel();
	private static HTML chainHTML = new HTML();
	private static HTML onooHTML = new HTML();
	private static Timer timer = new Timer() {
		public void run() {
			time--;
			if (time > 0) {
				schedule(1000);
			} else {
				bonus = tootsoh = 0;
			}
			setHTML();
		}
	};

	private static int fibonacci(int n) {
		if (n > 1) {
			return fibonacci(n - 1) + fibonacci(n - 2);
		} else {
			return n;
		}
	}

	public static VerticalPanel getPanel() {
		setHTML();
		panel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		panel.add(onooHTML);
		panel.add(chainHTML);
		return panel;
	}

	public static int getSpeed() {
		double x = mor / 200;
		double y = java.lang.Math.pow(x * 6 + 1, -x);
		return (int) (1000 * y) + 1;
	}

	public static void incLines(int n) {
		tootsoh += n;
		bonus += n * fibonacci(tootsoh) * (mor + 10);
		onoo += n * fibonacci(tootsoh) * (mor + 10);
		mor += n;
		if (time == 0) {
			timer.schedule(getSpeed());
		}
		time += 3;
		setHTML();
	}

	private static void setHTML() {
		onooHTML.setHTML("Мөрүүд: " + mor.toString() + " | Оноо: "
				+ onoo.toString());
		if (time > 0) {
			chainHTML.setHTML("<b>Цуваа</b>: " + tootsoh.toString() + " мөрүүд, +"
					+ bonus.toString() + " оноо (<font color=red>"
					+ time.toString() + "</font>)");
		} else {
			chainHTML.setHTML("");
		}
	}
}
